<?php

/**
 * Collection of utility methods
 *
 * PHP version 5
 *
 * LICENSE: This source file is subject to the MIT License, available
 * at http://www.opensource.org/licenses/mit-license.html
 *
 * @author     Jayson Schmidt <jayson@888mobileapps.com>
 * @copyright  2013 888MobileApps
 * @license    http://www.opensource.org/licenses/mit-license.html
 */

class Utility {
	
	/*
	 * Updates $col in $table with $update based on $pid
	 *
	 * @param obj $db The db to use
	 * @param string $table The table to be updated
	 * @param string $col The colummn to be updated
	 * @param int $pid The row to be updated
	 * @param mixed $update The value to use for the update
	 *
	 * @return mixed TRUE on succes, error message on failure
	 */
	public static function updateByPid($db, $table, $col, $pid, $update) {
		
		$pid = (int)$pid;
		$update = htmlentities($update);
		
		$sql = "UPDATE 
					`".$table."`
				SET
					`".$table."`.`".$col."` = '".$update."'
				WHERE
					`".$table."`.`pid` = '".$pid."'";	
		
		try {
			$stmt = $db->prepare($sql);
			
			$stmt->execute();
			$stmt->closeCursor();
			return TRUE;
		}catch (Exception $e) {
			return $e->getMessage();
		}
		
	}
	
	/*
	 * Updates $col in $table with $updates based on $pid
	 *
	 * @param object $db The db to use
	 * @param string $table The table to be updated
	 * @param string $col The colummn to be updated
	 * @param int $pid The row to be updated
	 * @param array $updates The array to use for the update
	 *
	 * @return mixed TRUE on succes, error message on failure
	 */
	public static function updateByArray($db, $table, $col, $pid, $updates) {
	
		if(!is_array($updates))
			return "Invalid array supplied to method updateByArray";
		
		$sql = "Update
					`".$table."`
				SET
					`".$table."`.`".$col."` = :mid
				WHERE
					`".$table."`.`pid` = :tid";
		
		
		try {
			
			$stmt = $db->prepare($sql);
			
			foreach($updates as $key=>$value) {
				
				$mid = (int)$value;
				$tid = (int)$key;
			
				$stmt->bindParam(":mid", $mid, PDO::PARAM_INT);
				$stmt->bindParam(":tid", $tid, PDO::PARAM_INT);
				$stmt->execute();
			}

			$stmt->closeCursor();
			return TRUE;
		}catch (Exception $e) {
			return $e->getMessage();
		}
	
	
	}
	
	/*
	 * Selects from $selections from $table using $criteria
	 *
	 * @param object $db The db to use
	 * @param string $table The table to select from
	 * @param array $selections An array of selections to be made (ie, $selections(0=>'first', 1=>'second'), etc)
	 * @param array $critera An array of selection criteria (ie, $criteria('some_name'=>'this_name')
	 */
	public static function selectByArray($db, $table, $selections, $criteria) {
		
		if(!is_array($selections) || !is_array($criteria))
			return 'Invalid array paramters supplied to selectByArray';
		
		$sql = "SELECT ";
		
		$counter = 1;
		$selectionsSize = count($selections);
		foreach($selections as $key=>$value) {
			$sql .= "`".$table."`.`".$value."`";
			if($counter < $selectionsSize)
				$sql .= ',';
			$counter++;
		}
		
		$sql .= " FROM `".$table."`";
		
		$sql .= " WHERE ";
		
		$counter = 1;
		$criteriaSize = count($criteria);
		foreach($criteria as $key=>$value) {
			$sql .= "`".$table."`.`".$key."` = '".$value."'";
			if($counter < $criteriaSize)
				$sql .= ' AND ';
			$counter++;
		}		
			
		$stmt = $db->prepare($sql);
		$stmt->execute();
		$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
		$stmt->closeCursor();
		
		return $results;
	}
	

}


?>